var path = window.location.href;
var param = path.split("?")
var a=param[1].split("&")
var i=a[0].split("=")
var id=i[1].split(",")
var tit=a[1].split("=")
var title=tit[1].split(",")
var open=a[2].split("=")
var openTime=open[1].split(",")
var end=a[3].split("=")
var endTime=end[1].split(",")
var image_url=decodeURIComponent(decodeURIComponent(decodeURIComponent(getQueryString("url_path"))))
var image_path=decodeURIComponent(decodeURIComponent(decodeURIComponent(getQueryString("image_path"))))
var r=null
var pingtai
var CangName=""
var code=""
var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

$.each(warehouses.data,function(k,s){
    code +=s.warehouseCode+ ","
    CangName+=s.warehouseName+"/"

})
//去掉最后一个逗号(如果不需要去掉，就不用写)
if (code.length > 0) {
    code = code.substr(0, code.length - 1);
}
if (CangName.length > 0) {
    CangName = CangName.substr(0, CangName.length - 1);
}
if (getQueryString("category_id")!=3){
    $("#div").hide()
}
if (getQueryString("category_id")==3){
    $("#div").show()
    $('#more').text('拉幕图片大小限制为1200*500');
}
if (getQueryString("category_id")==4){
    $('#more').text('浮框图片大小限制为1920*120');
}
if (getQueryString("category_id")==5){
    $('#more').text('弹框图片大小限制为400*300');
}
$(function () {
    $.ajax({
        type: "post",
        url: root.sroreinter.search_resource_conf,
        cache: false,  //禁用缓存
        dataType: "json",
        data: {
            resource_id:getQueryString("id")
        },  //传入组装的参数
        success: function (data) {
            if(data.errorCode=="0") {
                $("#logmin").val(data.data[0].started_at)
                $("#logmax").val(data.data[0].end_in)
                $("#ordinaryUrl").val(data.data[0].ordinary_image_url)
                code=data.data[0].warehouse_code;
                code=code.split(',')
                var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
                for(var i=0;i<code.length;i++){//id相同为选中状态
                    console.log(code[i])
                    if(code.indexOf(code[i])>= 0){
                        $.each(warehouses.data,function(k,s){
                            if(code[i]==s.warehouseCode){
                                var warecode= document.getElementById('ying'+s.warehouseCode+'')
                                warecode.checked=true
                            }
                        })
                    }
                }
                if(code.indexOf("49") >= 0 ) {
                    $("#jiangsu").attr("checked","checked")
                }
                if(code.indexOf("50") >= 0 ) {
                    $("#wuhan").attr("checked","checked")
                }
                code=data.data[0].warehouse_code;
                CangName=data.data[0].warehouse_name;
                pingtai=data.data[0].platform;
                if (pingtai=='10'){
                    $("#qianyue").attr("checked","checked")
                    $("#url").css('display','inline');
                    $("#ordinaryUrl").css('display','none');
                }else if (pingtai=='20'){
                    $("#putong").attr("checked","checked")
                    $("#ordinaryUrl").css('display','inline');
                    $("#url").css('display','none');
                }else if (pingtai=='10,20'){
                    $("#ordinaryUrl").css('display','inline');
                    $("#putong").attr("checked","checked")
                    $("#url").css('display','inline');
                    $("#qianyue").attr("checked","checked")
                }
            }
        }
    })
})
$(function () {
    $("#biaoti").val(decodeURI(title))
    $("#color_value").val(getQueryString("color_value"))
    $("#url").val(image_url)
    $("#myImg").attr("src",image_path)
})
function changImg(e){
    var image = new Image();
    for (var i = 0; i < e.target.files.length; i++) {
        var file = e.target.files.item(i);
        if (!(/^image\/.*$/i.test(file.type))) {
            layer.msg('请上传图片!', {icon: 6, time: 2000});
            $("#file").val("")
            continue; //不是图片 就跳出这一次循环
        }
        //实例化FileReader API
        var freader = new FileReader();
        freader.readAsDataURL(file);
        freader.onload = function(e) {
            image.src =e.target.result ;
            image.onload = function() {
                if(file.size>5*1024*1024){
                    layer.msg('图片不得大于5M!', {icon: 6, time: 2000});
                    $("#file").val("")
                    r=0
                    return;
                }else{
                    var element = $(e.target).next();
                    if (getQueryString("category_id")==3){
                        if (this.width!=1200||this.height!=500){
                            r=1
                            layer.msg('拉幕图片大小限制为1200*500!', {icon: 6, time: 2000});
                            $("#file").val("")
                            return;
                        }else {
                            $("#myImg").css({width:this.width/5+"px",height:this.height/5+"px",cursor:"pointer"});
                            $("#myImg").attr("src",e.target.result);
                        }
                    }
                    if (getQueryString("category_id")==4){
                        if (this.width!=1920||this.height!=120){
                            r=1
                            layer.msg('浮框图片大小限制为1920*120!', {icon: 6, time: 2000});
                            $("#file").val("")
                            return;
                        }else {
                            $("#myImg").css({width:this.width/5+"px",height:this.height/5+"px",cursor:"pointer"});
                            $("#myImg").attr("src",e.target.result);
                        }
                    }
                    if (getQueryString("category_id")==5){
                        if (this.width!=400||this.height!=300){
                            r=1
                            layer.msg('弹框图片大小限制为400*300!', {icon: 6, time: 2000});
                            $("#file").val("")
                            return;
                        }else {
                            $("#myImg").css({width:this.width+"px",height:this.height+"px",cursor:"pointer"});
                            $("#myImg").attr("src",e.target.result);
                        }
                    }
                }
            }
        }
    }
}
function changePingtai() {
    $("#putong").css('outline','')
    $("#qianyue").css('outline','')

    if ($("#qianyue").is(":checked")&&$("#putong").is(":checked")){
        $("#url").css('display','inline');
        $("#ordinaryUrl").css('display','inline');
        pingtai='10'+","+'20'
    }
    if ($("#qianyue").is(":checked")&&!$("#putong").is(":checked")){
        $("#url").css('display','inline');
        $("#ordinaryUrl").css('display','none');
        pingtai='10'
    }
    if ($("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#url").css('display','none');
        $("#ordinaryUrl").css('display','inline');
        pingtai='20'
    }
    if (!$("#putong").is(":checked")&&!$("#qianyue").is(":checked")){
        $("#url").css('display','none');
        $("#ordinaryUrl").css('display','none');
        pingtai='0'
    }
}
function changeCang() {
    code=''
    CangName=''
    var warcode=""//id
    var warcodename=""//name
    var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据
    $.each(warehouses.data,function(k,s){
        if(document.getElementById('ying'+s.warehouseCode+'').checked==true){//获取选中的id
            warcode+=document.getElementById('ying'+s.warehouseCode+'').value+ ","//逗号拼接获取到的所以id
            warcodename+=document.getElementById('ying'+s.warehouseCode+'').name+ "/"
        }
    })
    //去掉最后一位的逗号
    if (warcode.length > 0) {
        warcode = warcode.substr(0, warcode.length - 1);
    }
    if (warcodename.length > 0) {
        warcodename = warcodename.substr(0, warcodename.length - 1);
    }
    code=warcode
    CangName=warcodename
}
$("#form-member").validate({
    rules: {
        biaoti:"titleCheck",
        color_value:"colorCheck",
    },
    //提交表单数据
    submitHandler:function(form){
        if (pingtai=='0'||code=='0'){
            if (pingtai=='0'){
                $("#putong").css('outline','1px solid red')
                $("#qianyue").css('outline','1px solid red')
            }
            if (code==''){
                var warehouses=JSON.parse(sessionStorage.getItem("warehouses"))//获取仓库数据

                $.each(warehouses.data,function(k,s){
                    var warecode= document.getElementById('ying'+s.warehouseCode+'')
                    $(warecode).css('outline','1px solid red')

                })
                $("#hunan").css('outline','1px solid red');            $("#jinan").css('outline','1px solid red');            $("#shanxi").css('outline','1px solid red');            $("#guangdong").css('outline','1px solid red');$("#sichuan").css('outline','1px solid red');
            }
            layer.msg("请选择配置平台/地域？", {icon: 6, time: 3000});
            return false
        }
        var formData = new FormData();
        formData.append("started_at",$("#logmin").val());
        formData.append("resource_id",getQueryString("id"));
        formData.append("category_id",getQueryString("category_id"));
        formData.append("color_value",$("#color_value").val());
        formData.append("end_in",$("#logmax").val());
        formData.append("image_url",$("#url").val());
        formData.append("ordinary_image_url",$("#ordinaryUrl").val());
        formData.append("title",$("#biaoti").val());
        if (r==null){
            formData.append("file",$("#file")[0].files[0]);
        }
        formData.append("last_modified_name",JSON.parse(web.readCookie("loginData")).user.username);
        formData.append("last_modified_by",JSON.parse(web.readCookie("loginData")).user.user_ID);
        /*平台和仓库*/
        formData.append("platform",pingtai);
        formData.append("warehouse_code",code);
        formData.append("warehouse_name",CangName);
        console.log($("#file")[0].files[0])
        $.ajax({
            url: root.sroreinter.save_resource_conf,
            cache: false,  //禁用缓存
            type: "POST",
            // 告诉jQuery不要去处理发送的数据
            processData : false,
            // 告诉jQuery不要去设置Content-Type请求头
            contentType : false,
            data: formData,
            success: function (data) {
                console.log(data)
                if (data.errorCode=="92"){
                    layer.msg('您添加的开始时间小于已存在数据的结束时间!', {icon: 6, time: 3000});
                }
                if (data.errorCode=="94"){
                    layer.msg(data.errorMessage, {icon: 6, time: 3000});
                }
                if(data.errorCode=="0") {
                    layer.msg('操作成功!', {icon: 6, time: 1000});
                    parent.isEdit = true;
                    parent.queryDataFn();
                    setTimeout( function () {
                        cancelLayerNoRefreshFn();
                    },700);
                }
                if (data.errorCode=="93"){
                    layer.msg('开始时间不能大于结束时间,请重新查看', {icon: 6, time: 3000});
                    return;
                }
            }
        });
    }
})

//初始化客户归属下拉列表
function ascription() {
    var user_data = {}
    var succCallBack = function (data, status, response) {
        if (data.errorCode == "0") {
            var dataInter = data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}
function removeIframe(){
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
}
$('#pic').click(function (event) {
    event.preventDefault()
    $('#file').click()
})